package org.db.admin.modules.sys.controller;


import org.db.admin.common.LayuiRes;
import org.db.admin.modules.sys.entity.SysMenu;
import org.db.admin.modules.sys.service.SysMenuService;
import org.db.admin.modules.sys.util.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author db117
 * @since 2018-02-17
 */
@Controller
@RequestMapping("/sysMenu")
public class SysMenuController extends SuperController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 进入列表页面
     */
    @GetMapping(value = "list")
    public String list() {
        return "sys/menu/list";
    }

    @GetMapping(value = "listTest")
    @ResponseBody
    public LayuiRes listTest() {
        List<SysMenu> list = new ArrayList<>();
        List<SysMenu> sourcelist = sysMenuService.selectList(null);
        MenuUtil.sortList(list, sourcelist, "0", true);
        return LayuiRes.getPageRes(1, list);
    }

    /**
     * 进入添加页面
     */
    @GetMapping(value = "add")
    public String add() {
        return "sys/menu/add";
    }

    /**
     * 添加跟目录
     */
    @PostMapping(value = "add")
    public LayuiRes add(int root, SysMenu sysMenu) {
        if (root == 1) {
            sysMenu.setParentId("0");
            sysMenu.setParentIds("0");
            sysMenuService.insert(sysMenu);
        }
        return LayuiRes.getSuccess("");
    }

    public String tree() {
        return "";
    }
}

